package com.example.photographWatermark.camera2

import android.app.Activity
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.WindowManager
import androidx.annotation.RequiresApi
import com.example.photographWatermark.MainActivity
import com.example.photographWatermark.R
import kotlinx.android.synthetic.main.activity_camera2.*

/**
 * author :  chensen
 * data  :  2018/3/18
 * desc :
 */

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
class CameraActivity : Activity() {

    private lateinit var mCamera2Helper: CameraHelper
    companion object {
        const val TOGGLE_FLASH = 200
        const val CANCEL = 300
        const val EXTRA_CONFIG = "config"
        const val EXTRA_RESULT = "scan_result"
        const val EXTRA_ERROR_CODE = "error_code"
    }
    
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_camera2)
        window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
        val name = intent.getStringExtra("name")
        val address = intent.getStringExtra("address")
        val  time = intent.getStringExtra("time")
//        Log.d("=========================","name==$name")
        val bean = CameraBean(address, name, time)
        mCamera2Helper = CameraHelper(this, textureView, imageCallback, bean)

        btnTakePic.setOnClickListener {
            mCamera2Helper.takePic()
        }

    }

    override fun onDestroy() {
        super.onDestroy()
        mCamera2Helper.releaseCamera()
        mCamera2Helper.releaseThread()
    }

    private val imageCallback: ImageCallback = object : ImageCallback {

        override fun onSuccess(bitmapPath: String) {
            Log.d("----", "图片返回成功：${bitmapPath}")
            val intent = Intent(this@CameraActivity, MainActivity::class.java)
//            val intent = Intent()
//            intent.action = "main"
            intent.putExtra("camera", bitmapPath)
//            intent.putExtra("name", name)
//            intent.putExtra("address", address)
//            intent.putExtra("time", time)
            startActivity(intent)
            finish()
//                intent.setData()
//            intent.putExtra("camera", bitmapPath)
//            setResult(Activity.RESULT_OK,intent)
//                startActivityForResult(intent,1)
          
        }

    }
//    private val onImageAvailableListener = ImageReader.OnImageAvailableListener {
//
//        val image = it.acquireNextImage()
//        val byteBuffer = image.planes[0].buffer
//        val byteArray = ByteArray(byteBuffer.remaining())
//        byteBuffer.get(byteArray)
//        it.close()
//        Log.d("---", "图片保存")
//
//        val temp = System.currentTimeMillis()
//        val picFile = FileUtil.createCameraFile("camera2")
//        if (picFile != null && byteArray != null) {
//
//            val rawBitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.size)
//            val resultBitmap = if (isMirror) BitmapUtils.mirror(rawBitmap) else rawBitmap
//
//
//            picFile.sink().buffer().write(BitmapUtils.toByteArray(resultBitmap)).close()
//
//        }
//    }
}

